package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysDictType;
import com.blb.zycommunity.entity.vo.SysDictTypeExeclWrite;
import com.blb.zycommunity.entity.vo.SysDictTypeVo;
import com.blb.zycommunity.mapper.SysDictTypeMapper;
import com.blb.zycommunity.service.ISysDictTypeService;
import com.blb.zycommunity.utils.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */

@RestController
public class SysDictTypeController {

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    @Resource
    private SysDictTypeMapper sysDictTypeMapper;

    //查询所有加分页
    @PostMapping("dictType-page")
    public ResponseResult findPageAll(@RequestBody SysDictTypeVo sysDictTypeVo){
        Page<SysDictType> page = new Page<>(sysDictTypeVo.getPageNum(),sysDictTypeVo.getPageSize());

        Page<SysDictType> sysDictTypePage = sysDictTypeMapper.selectAllPage(page,
                sysDictTypeVo.getDictName(), sysDictTypeVo.getDictType(),
                sysDictTypeVo.getStatus(), sysDictTypeVo.getStartTime(),
                sysDictTypeVo.getFinishTime());
        return ResponseResult.ok(sysDictTypePage);
    }

    /**
     * 新增
     * @return
     */
    @PostMapping("dictType-add")
    public ResponseResult addDictType(@RequestBody SysDictType sysDictType){
        LocalDateTime now = LocalDateTime.now();
        sysDictType.setCreateTime(now);
        iSysDictTypeService.save(sysDictType);
        return ResponseResult.ok("ok");
    }

    /**
     * 修改
     * @param sysDictType
     * @return
     */
    @PutMapping("dictType-update")
    public ResponseResult updateType(@RequestBody SysDictType sysDictType){
        LocalDateTime now = LocalDateTime.now();
        sysDictType.setUpdateTime(now);
        iSysDictTypeService.updateById(sysDictType);
        return ResponseResult.ok("ok");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/dictType/{ids}")
    public ResponseResult deleteById(@PathVariable Long[] ids){
        for (Long id : ids) {
            iSysDictTypeService.removeById(id);
        }
        return ResponseResult.ok("删除成功");
    }


    /**
     * 根据id导出数据
     * @param response
     * @param ids
     * @param filename
     * @throws IOException
     */
    @GetMapping("downloads-dictType")
    public void exports(
            HttpServletResponse response,
            @RequestParam("ids") long[] ids,
            @RequestParam("filename") String filename) throws IOException {
        System.out.println(filename);
        //List<Long> list = ExcelDownloadUtil.toList(ids);
        List<SysDictType> sysDictTypes = new ArrayList<>();
        for (Long id : ids) {
            SysDictType byId = iSysDictTypeService.getById(id);
            sysDictTypes.add(byId);
        }
        sysDictTypes.forEach(System.out::println);
        ExcelDownloadUtil.excelDownload(response, filename, SysDictTypeExeclWrite.class, sysDictTypes);
    }



}
